package com.xuzz.study.io.mynetty;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;

/**
 * @Description
 * @Date 2023/4/5 20:31
 * @Created by xuzz3@asiainfo-sec.com
 */
public class ServerBootStrap {
    ThreadSelectorGroup bossGroup;
    ThreadSelectorGroup workerGroup;
    public ServerBootStrap(ThreadSelectorGroup boss,ThreadSelectorGroup worker) {
        this.bossGroup = boss;
        boss.setBootStrap(this);

        this.workerGroup = worker;
        worker.setBootStrap(this);
    }

    public void bind(InetSocketAddress address) {
        for (int i=0;i<this.bossGroup.getThreads().length;i++){
            try {
                ThreadSelector thread = this.bossGroup.getThreads()[i];
                ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
                serverSocketChannel.configureBlocking(false);
                serverSocketChannel.bind(address);
                thread.getTasks().put(serverSocketChannel);
                thread.getSelector().wakeup();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
